﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Streambolics.Gui;
using Streambolics.Gui.Gpl;

namespace Streambolics.App.Gpl
{
    public delegate bool ModuleAction (ComputingModule m);

    public partial class OlvModuleChildren : StandardUserControl
    {
        private ComputingModule _Module;

        public OlvModuleChildren ()
        {
            InitializeComponent ();
            column_Name.AspectToStringConverter = delegate (object x)
            {
                return "";
            };
        }

        public ComputingModule Module
        {
            get
            {
                return _Module;
            }
            set
            {
                if (value != _Module)
                {
                    if (_Module != null)
                    {
                        _Module.ChildStatusChanged -= new EventHandler (_Module_ChildStatusChanged);
                    }
                    _Module = value;
                    if (_Module != null)
                    {
                        _Module.ChildStatusChanged += new EventHandler (_Module_ChildStatusChanged);
                    }
                    _Module_ChildStatusChanged (this, EventArgs.Empty);
                }
            }
        }


        void _Module_ChildStatusChanged (object sender, EventArgs e)
        {
            list.ClearObjects ();
            if (_Module != null)
            {
                list.SetObjects (_Module.Children);
            }
        }

        private void Apply (ModuleAction aAction, string aActionName)
        {
            int count = 0;

            foreach (object o in list.SelectedObjects)
            {
                if (aAction ((ComputingModule)o))
                {
                    count++;
                }
            }

            if (count > 0)
            {
                //MessageBox.Show (String.Format (aActionName, count), "Module Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show (String.Format (aActionName, "None"), "Module Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void StartSelected ()
        {
            Apply (delegate (ComputingModule m)
            {
                if (!m.IsRunning)
                {
                    m.Start ();
                    return true;
                }
                else
                {
                    return false;
                }
            }, "Modules started: {0}");
        }

        public void StopSelected ()
        {
            //  TODO : Ask confirmation...

            Apply (delegate (ComputingModule m)
            {
                if (m.IsRunning)
                {
                    m.Stop ();
                    return true;
                }
                else
                {
                    return false;
                }
            }, "Modules stopped: {0}");
        }

        public void DetailSelected ()
        {
            Apply (delegate (ComputingModule m)
            {
                OlvModuleDetails.Open (m);
                return true;
            }, "Modules shown: {0}");
        }

        private void menuItem_Start_Click (object sender, EventArgs e)
        {
            StartSelected ();
        }

        private void menuItem_Stop_Click (object sender, EventArgs e)
        {
            StopSelected ();
        }

        private void menuItem_Details_Click (object sender, EventArgs e)
        {
            DetailSelected ();
        }

        private void list_DoubleClick (object sender, EventArgs e)
        {
            DetailSelected ();
        }

        private void list_FormatCell (object sender, BrightIdeasSoftware.FormatCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                ComputingModule m = (ComputingModule)e.Model;
                NamedDescriptionDecoration decoration = new NamedDescriptionDecoration ();
                decoration.ImageList = imageList_48;

                if (m.IsFailed)
                {
                    decoration.ImageName = "Error";
                }
                else if (m.IsRunning)
                {
                    decoration.ImageName = "Running";
                }
                else
                {
                    decoration.ImageName = "Stopped";
                }

                decoration.Title = m.Name;
                decoration.Description = m.ArgumentsText;
                e.SubItem.Decoration = decoration;
            }

        }
    }
}
